﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using octopus_project.scene;
using octopus_project.enumeration;
using octopus_project.tileset.mapCells;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using octopus_project.scene.cannon.weapon;

namespace octopus_project.tileset {
    abstract class  MapCell {

        private StrongCell strongCell;

        

        private TileType TileType;
        public bool chequed = false;
        private bool Conquered = false;
        public bool castle = false;
        public int x;
        public int y;
        public Player player;

        public bool conquered {
            get { return Conquered; }
            set {
                Conquered = value;
                changeConquered();
            }
        }

        public StrongCell StrongCell {
            get {
                if (strongCell == null) {
                    strongCell = new StrongCell(5);
                }
                return strongCell;
            }
            set { strongCell = value; }
        }
        /// <summary>
        /// Description.
        /// </summary>
       public void setValues(MapCell mapCell) {
           this.chequed = mapCell.chequed;
           this.conquered = mapCell.conquered;
           this.castle = mapCell.castle;
           this.x = mapCell.x;
           this.y = mapCell.y;
           this.player = mapCell.player;
       }


        public TileType tileType {
            get { return TileType; }
            set {
                if (TileType != TileType.PLAYER_CASTLE)
                    TileType = value; 
             } 
            
        }

        public static MapCell createMapCell(TileType tile){
            switch (tile) {

                case TileType.PLAYER_CONQUERED: return new Conquered_MapCell();
                case TileType.PLAYER_WALL: return new Wall_MapCell();
                case TileType.PLAYER_CURSOR: return new Cursor_MapCell();
                case TileType.PLAYER_CASTLE: return new Castle_MapCell();
                case TileType.NOT_CONQUERED: return new NotConquered_MapCell();
                case TileType.UNAVAILABLE: return new Unavailable_MapCell();
                case TileType.DESTROY: return new Destroy_MapCell();
                
            }
            
            return null;
        }


        public virtual void paint(SpriteBatch spriteBatch, int x, int y, TileMap_GameplayEntry map) {
            PlayersColor playerColor = PlayersColor.UNDEFINED;
            if (player != null) {
                playerColor = player.Color;
            }

                spriteBatch.Draw(
                    TileTypeExtensions.SpriteImage,
                    new Rectangle(x * Constant.TILE_SIDE, y * Constant.TILE_SIDE, Constant.TILE_SIDE, Constant.TILE_SIDE),
                    TileTypeExtensions.GetSourceRectangle(tileType, playerColor),
                    Color.White);
           
        }

        public virtual MapCell shootMe(Damage damage) {

            if (StrongCell != null && StrongCell.damage(damage.Value)) {

                return change(TileType.DESTROY);
            }

            return this;
        }

        public virtual MapCell build() {
            return this;
        }

        public virtual bool isEnabledToBuild() {
            return false;
        }
        public virtual bool isWall() {
            return false;
        }
        public virtual bool isWeapon() {
            return false;
        }
        public virtual MapCell change(TileType tileTypeParam) {
            MapCell mapCell = createMapCell(tileTypeParam);

            mapCell.setValues(this);

            Game1.battlegroundMap.setMapCell(mapCell.x, mapCell.y, mapCell);

            return mapCell;
        }

        /// <summary>
        /// Cuando cambia el estado conquistado del tile, es llamado este metodo.
        /// </summary>
        public virtual void changeConquered() { }


        public static bool createCellCannon(byte x, byte y, Player playerParam, Weapon weapon) {
            if (weapon is MediumCannon) {
                return MediumCannon_MapCell.createCannon(x, y, playerParam, (MediumCannon)weapon);
            }
            else if (weapon is MiniCannon) {
                MapCell newMapcell = new MiniCannon_MapCell((MiniCannon)weapon);

                MapCell mapCellOld = Game1.battlegroundMap.getMapCell(x, y);

                if (mapCellOld is Conquered_MapCell) {
                    newMapcell.setValues(mapCellOld);
                    newMapcell.player = playerParam;

                    Game1.battlegroundMap.setMapCell(x, y, newMapcell);
                    return true;
                }
                
            }
            return false;

        }
       
    }
}
